package MyCustomTable;

import MyUtil.DataBaseUtil;
import MyUtil.JScrollUtil;
import MyUtil.MyPanel;
import MyUtil.ResultSetTableModel;

import javax.swing.*;
import java.awt.*;
import java.sql.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Book_addPay_List extends JFrame{
    //date转换
    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    //编号
    private int pid = -1;
    //sql语句
    private String sq = "select * from Pay_List";
    private String sql ="select Clevel from Customer where Cid = ?";
    private String sql2 = "select Caccount from Member where Clevel= ?";
    private String sql3 = "select Bpay,Bnumber from Book where Bid = ?";
    private String sql4 = "update Book set Bnumber =Bnumber-? where Bid = ?";
    private String sql5 = "insert into Pay_List values(?,?,?,?,?,?)";
    private String sql6 = "select max(Pid) from Pay_List ";

    private JScrollPane jScrollPane;
    private JButton change_button = new JButton("添加");
    //标签
    private JLabel cid_label = new JLabel("顾客号ID:");
    private JLabel bid_label = new JLabel("书号ID:");
    private JLabel sell_label = new JLabel("购买数量:");
    private JLabel time_label = new JLabel("购买时间:");
    private JLabel year = new JLabel("年");
    private JLabel month = new JLabel("月");
    private JLabel day = new JLabel("日");
    //文本域
    private JTextField cid_TextField = new JTextField(8);
    private JTextField bid_TextField = new JTextField(8);
    private JTextField sell_TextField = new JTextField(4);

    private JTextField year_field = new JTextField(3);
    private JTextField month_field = new JTextField(2);
    private JTextField day_field = new JTextField(2);

    private JPanel myPanel;
    private JPanel myPanel2;
    public Book_addPay_List() throws SQLException {
        super("出售书籍");
        //基础设置
        this.setBounds(400,200,1200,600);
        //设置控件
        myPanel = new MyPanel();
        this.setContentPane(myPanel);
        myPanel2 = new MyPanel("/picture/3.jpg");
        //设置布局
        myPanel.setLayout(new BorderLayout());
        myPanel2.setLayout(new FlowLayout());
        //ScrollPane的设置
        jScrollPane = JScrollUtil.getCustomJScrollPane(DataBaseUtil.produceResultSet(sq));
        //加入控件
        myPanel.add(jScrollPane,BorderLayout.WEST);
        myPanel.add(myPanel2,BorderLayout.EAST);

        myPanel2.add(cid_label);
        myPanel2.add(cid_TextField);
        myPanel2.add(bid_label);
        myPanel2.add(bid_TextField);
        myPanel2.add(sell_label);
        myPanel2.add(sell_TextField);
        myPanel2.add(time_label);
        myPanel2.add(year_field);
        myPanel2.add(year);
        myPanel2.add(month_field);
        myPanel2.add(month);
        myPanel2.add(day_field);
        myPanel2.add(day);

        myPanel2.add(change_button);

        //设置Pid
        pid = getFirstPay_ListPid();
        //button设置
        change_button.addActionListener((e)->{
            String cid = cid_TextField.getText();
            String bid = bid_TextField.getText();
            int sell_num = Integer.parseInt(sell_TextField.getText());

            int level = -1; //客户等级
            float pay = -1;//书单价
            int number = -1;//书数量
            float discount = -1;//折扣

            float total_price = -1;
            java.util.Date date1 = null;
            if((bid.equals("")) ||(cid.equals(""))||(sell_TextField.equals(""))){
                JOptionPane.showMessageDialog(this,"有输入值为空","错误",JOptionPane.PLAIN_MESSAGE);
            }
            else{
                try {
                    ResultSet rs1 = DataBaseUtil.prepareResultSet(sql,cid);
                    ResultSet rs2 = DataBaseUtil.prepareResultSet(sql3,bid);
                    while(rs1.next()){
                        level =rs1.getInt(1);
                    }
                    while(rs2.next()){
                        pay =rs2.getFloat("Bpay");
                        number  =rs2.getInt("Bnumber");
                    }
                    //当前阶段都可以找到值
                    if (number!=-1 &&pay!=-1&&level!=-1){
                        ResultSet rs3 = DataBaseUtil.prepareResultSet(sql2,level);
                        while(rs3.next()){
                            discount =rs3.getFloat(1);
                        }
                        if(discount != -1) {
                            if (number >= sell_num) {
                                //删除书籍数量
                                Object[] o1 = new Object[2];
                                o1[0] = sell_num;
                                o1[1] = bid;
                                total_price = pay * discount * sell_num;
                                DataBaseUtil.changeResultSet(sql4, o1);
                                //添加数据
                                Object[] o2 = new Object[6];
                                o2[0] = pid;
                                o2[1] = cid;
                                o2[2] = bid;
                                o2[3] = sell_num;
                                o2[4] = total_price;
                                o2[5] = produceDate();
                                DataBaseUtil.changeResultSet(sql5, o2);
                                pid = getNextPay_ListPid();
                                //显示东西了
                                myPanel.remove(jScrollPane);
                                jScrollPane = JScrollUtil.getCustomJScrollPane(DataBaseUtil.produceResultSet(sq));
                                myPanel.add(jScrollPane, BorderLayout.WEST);
                                this.setVisible(true);
                            } else {
                                JOptionPane.showMessageDialog(this, "书记数量不足");
                            }
                        }
                    }
                    else{
                        JOptionPane.showMessageDialog(this,"信息不正确或不符合要求");
                    }
                } catch (SQLException | ParseException ex) {
                    ex.printStackTrace();
                }

            }
        });

        this.setVisible(true);

    }
    private java.sql.Date produceDate() throws ParseException {
        String s1  = year_field.getText()+"-"+month_field.getText()+"-"+day_field.getText();
        java.util.Date d1 = simpleDateFormat.parse(s1);
        java.sql.Date date1 = new java.sql.Date(d1.getTime());
        return date1;
    }
    private int getFirstPay_ListPid() throws SQLException {
        ResultSet rs = DataBaseUtil.produceResultSet(sql6);
        int n=0;
        while(rs.next()){
            n = rs.getInt(1)+1;
        }
        return n;
    }
    private int getNextPay_ListPid(){
        if(pid!=-1) {
            return pid+1;
        } else{
            return -1;
        }
    }
}
